﻿// DibBase.h: interface for the CDibBase class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DIBBASE_H__6B5330D4_B842_4037_8BEB_7F2B4570BF59__INCLUDED_)
#define AFX_DIBBASE_H__6B5330D4_B842_4037_8BEB_7F2B4570BF59__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "IDib.h"
#include "afxmt.h"

class CDibBase  : public IKmUnknownImpl<IDib>
{
public:
	CDibBase();
	virtual ~CDibBase();

protected:
	void*		m_pBits;
	void*		m_pBufBits;
	INT_PTR		m_nLength;
	int			m_nBitCount;

	CSize		m_Size;

	COLORREF*	m_p8BitColors;

	//COLORREF	m_a8BitColors[256];
	typedef struct 
	{
		BITMAPINFO bm;
		RGBQUAD	bmiColors[255];
	}DIB_BITMAPINFO;

	typedef struct
	{
		int nSectionRowCount, iFile, nTotalRowCount, nBeginRow;
		CString sFileName;
		LOAD_PROGRESS_CALLBACK lpCallback;
		LPVOID lpPara;
		CDibBase* pThis;
	}THREAD_LOAD_PARA;
	BYTE		m_bFinishs[4];
	CMutex		m_mutex;
	int			m_iFinishSection;
	BYTE*		m_aFinishSections;
	typedef struct
	{
		int nSectionRowCount, iFile, nTotalRowCount;
		CString sFileName;
		CDibBase* pThis;
	}THREAD_SAVE_PARA;

	BOOL		m_bCreateFromJpg;

public:
	virtual BOOL Create(int nWidth, int nHeight, int nBitCount);
	virtual SIZE Size(){ return m_Size; }
	virtual int* GetBits(INT_PTR* pLength)
	{
		if( pLength )
			*pLength = m_nLength;
		return (int *)m_pBits;
	}
	virtual int GetBitCount(){ return m_nBitCount; }

	virtual BOOL SaveToFile(LPCTSTR lpFileName);
	virtual BOOL LoadFromFile(LPCTSTR lpFileName);
	virtual BOOL LoadFromFile(CFile& file, int nRowCountPerTime = 0, LOAD_PROGRESS_CALLBACK lpCallback = NULL, LPVOID lpPara = NULL, int nBeginRow = 0);
	virtual BOOL LoadFromFile2(LPCTSTR lpFileName, LPCTSTR lpFileName2, LPCTSTR lpFileName3, LPCTSTR lpFileName4, LOAD_PROGRESS_CALLBACK lpCallback = NULL, LPVOID lpPara = NULL, int nBeginRow = 0);
	virtual void FillContent(int iValue);
	virtual void Destroy();

	virtual BOOL Contour();
	virtual BOOL Erosion(BYTE* pS, int sl, void* pCopyToBits, IProgressBar* pBar);
	virtual BOOL Dilation(BYTE* pS, int sl);
	virtual BOOL Open(BYTE* pS, int sl);
	virtual BOOL Close(BYTE* pS, int sl);
	virtual BOOL Thining();

	virtual BOOL CreateFromBmpFile(CString& sFileName);
	virtual BOOL SaveToBmpFile(CString& sFileName);
	virtual BOOL SavePartToFile(LPCTSTR lpFileName, int nRowCount);		// 把部分数据存入文件
	virtual BOOL SavePartToFile2(LPCTSTR lpFileName, LPCTSTR lpFileName2, LPCTSTR lpFileName3, LPCTSTR lpFileName4, int nRowCount, int nSectionRowCount);		// 把部分数据存入文件

	virtual BOOL SaveToJPGFile(CString sFileName, int nQuality);
	virtual void Mirror(BOOL bMirrorX, BOOL bMirrorY);

	virtual BOOL Serialize(CArchive& ar);

	virtual COLORREF Get8BitColor(int iIndex);
	virtual void Set8BitColor(int iIndex, COLORREF cr);
	virtual void Create8BitColor(COLORREF* pBasicCrs, int nNum);

	virtual BOOL SerializeJpg(CArchive& ar);
	virtual BOOL Copy(IDib* pSrc);
	virtual BOOL CreateFromJPGFile(CString sFileName);

protected:
	virtual BOOL DoCreate() = 0;
	void SetBitmapInfo(DIB_BITMAPINFO& bm, int nCy = 0);

private:
	bool MallocBufBits();
	void Homochromize(int value = 1);
	void DoErosion32(BYTE* pS, int sl, int* pSrcBits, int* pDestBits, CSize Size);
	void DoErosion8(BYTE* pS, int sl, BYTE* pSrcBits, BYTE* pDestBits, CSize Size, IProgressBar* pBar);
	void DoDilation(BYTE* pS, int sl, int* pSrcBits, int* pDestBits, CSize Size);
	int GetJpgQuality();

	INT_PTR RecLen( int nWidth, int nHeight, int nBitCount );

	BOOL DoLoadFromFile2(CFile& file, int nSectionRowCount, int iFile, int nTotalRowCount, LOAD_PROGRESS_CALLBACK lpCallback, LPVOID lpPara, int nBeginRow);
	static UINT ThreadLoadFromFile2(LPVOID lpPara);

	static UINT ThreadSavePartToFile2(LPVOID lpPara);
	BOOL DoSavePartToFile2(CString& sFileName, int nSectionRowCount, int iFile, int nTotalRowCount);
	BOOL Do_DoSavePartToFile2(CString& sFileName, int nSectionRowCount, int iFile, int nTotalRowCount);
};

#endif // !defined(AFX_DIBBASE_H__6B5330D4_B842_4037_8BEB_7F2B4570BF59__INCLUDED_)
